//
//  CustomSegmentBar.m
//  AiTuPianPad
//
//  Created by guo hua on 11-9-20.
//  Copyright (c) 2011年 __MyCompanyName__. All rights reserved.
//


#import "CustomSegmentBar.h"
#define BTN_TAG_BASE 100
@implementation CustomSegmentBar
@synthesize buttonList;
@synthesize delegate;
@synthesize images = _images;
@synthesize hImages = _hImages;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}
-(id)initWithImages:(NSArray *)images highlightedImages:(NSArray *)hImages{
    self = [super init];
    if (self) {
        [self setBackgroundColor:[UIColor clearColor]];

        self.images = images;
        self.hImages = hImages;
        self.buttonList = [NSMutableArray array];
        int i = 0;
        float width = 0;
        float height = 0;
        float x = 0;
        for (UIImage *img in images) {
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            [btn setImage:img forState:UIControlStateNormal];
            if (i<[hImages count]) {
                [btn setImage:[hImages objectAtIndex:i] forState:UIControlStateHighlighted];
            }
            [btn sizeToFit];
            btn.frame = CGRectMake(x, 0, btn.frame.size.width, btn.frame.size.height);
            [btn addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
            [buttonList addObject:btn];
            [self addSubview:btn];
            i++;
            
            width += btn.frame.size.width;
            x += btn.frame.size.width;
            height = btn.frame.size.height;
        }
        self.frame = CGRectMake(0, 0, width, height);
    }
    return self;
}
-(void)dealloc{
    self.buttonList = nil;
    self.images = nil;
    self.hImages = nil;
    selectedButton = nil;
    [super dealloc];
}
-(void)buttonClicked:(id)sender{
    if (selectedButton == sender) {
        return;
    }
    if (selectedButton) {
        [selectedButton setImage:[_images objectAtIndex:[buttonList indexOfObject:selectedButton]] forState:UIControlStateNormal];
        [selectedButton setImage:[_hImages objectAtIndex:[buttonList indexOfObject:selectedButton]] forState:UIControlStateHighlighted];
    }
    
    selectedButton = sender;
    
    [selectedButton setImage:[_hImages objectAtIndex:[buttonList indexOfObject:selectedButton]] forState:UIControlStateNormal];
    [selectedButton setImage:[_hImages objectAtIndex:[buttonList indexOfObject:selectedButton]] forState:UIControlStateHighlighted];
    if (delegate && [delegate respondsToSelector:@selector(segment:selectedIndexChanged:)]) {
        [delegate segment:self selectedIndexChanged:[buttonList indexOfObject:selectedButton]];
    }
}

-(void)selectedButtonAtIndex:(int)index{
    if (index<0||index >= [buttonList count] || selectedButton == [buttonList objectAtIndex:index]) {
        return;
    }
    if (selectedButton) {
        [selectedButton setImage:[_images objectAtIndex:[buttonList indexOfObject:selectedButton]] forState:UIControlStateNormal];
        [selectedButton setImage:[_hImages objectAtIndex:[buttonList indexOfObject:selectedButton]] forState:UIControlStateHighlighted];
    }
    UIButton *btn = [buttonList objectAtIndex:index];
    selectedButton = btn;
    
    [selectedButton setImage:[_hImages objectAtIndex:[buttonList indexOfObject:selectedButton]] forState:UIControlStateNormal];
    [selectedButton setImage:[_hImages objectAtIndex:[buttonList indexOfObject:selectedButton]] forState:UIControlStateHighlighted];
}
@end
